package com.itheima.info.manger.dao;

import com.itheima.info.manger.domain.Student;

public class StudentDao {
    //创建静态学生数组用于保存学生信息
    private static Student[] student = new Student[5];
    public boolean addStudent(Student stu) {
        //遍历数组判断数组是否满了,如果没有满把stu添加到student数组对应的index索引中.
        int index = -1;
        //遍历student数组,如果数组中元素为null,则将对应的索引i赋值给index.
        for (int i = 0; i < student.length; i++) {
            if (student[i] == null){
                index = i;
                break;
            }
        }
        //若果index等于-1,则说明数组已经存满了;否则将传递来的stu对象储存到student对应的index索引中.
        if (index == -1){
            return false;
        }else {
            student[index] = stu;
            return true;
        }
    }

    //创建findAllStudent()方法,将Student[]数组返回.
    public Student[] findAllStudent() {
        return student;
    }

    public boolean deleteStudent(String id) {
        //遍历数组中元素,判断传递来的id是否与数组中的id相同,如果相同将id对应索引下的元素变为null,并返回true;否则返回false.
        for (int i = 0; i < student.length; i++) {
            if (student[i] != null && student[i].getId().equals(id)){
                student[i] = null;
                return true;
            }
        }
        return false;
    }

    public boolean updateStudent(Student students) {
        //遍历数组中的元素,判断传递来的students数组id是否与student数组中的id相同,如果相同将students赋值给对应索引的student,并返回true;否则返回false.
        for (int i = 0; i < student.length; i++) {
            if (student[i] != null && student[i].getId().equals(students.getId())){
                student[i] = students;
                return true;
            }
        }
        return false;
    }
}
